package com.free.semantic.core.adaptor.impl;

import com.free.semantic.common.EqualFormula;
import com.free.semantic.common.RelationItem;
import com.free.semantic.core.adaptor.ConceptAppendAdaptor;
import com.free.semantic.core.ConceptManage;

import java.util.*;
import java.util.stream.Collectors;

public class ConceptAppendAdaptorImpl implements ConceptAppendAdaptor {

    private ConceptManage sourceConcept;

    private ConceptManage appendConcept;

    private List<EqualFormula> equalFormulas = new ArrayList<>();

    /**
     * first到second的映射
     */
    private final Map<String, String> variableFirstToSecondMap = new HashMap<>();

    private final Map<String, String> variableSecondToFirstMap = new HashMap<>();

    @Override
    public void setSourceConcept(ConceptManage source) {
        this.sourceConcept = source;
    }

    @Override
    public void setAppendConcept(ConceptManage append) {
        this.appendConcept = append;
    }

    @Override
    public void addEqualFormula(EqualFormula equalFormula) {
        equalFormulas.add(equalFormula);
        variableFirstToSecondMap.put(equalFormula.getLeftObjectName(), equalFormula.getRightObjectName());
        variableSecondToFirstMap.put(equalFormula.getRightObjectName(), equalFormula.getLeftObjectName());
    }

    @Override
    public void doAppend() {
        Set<String> modelObjLs = variableFirstToSecondMap.keySet();
        for (String firstFrom : modelObjLs) {

            for (String firstTo : modelObjLs) {
                if (firstFrom.equals(firstTo)) {
                    continue;
                }
                ConceptManage conceptManage = sourceConcept;

                List<RelationItem> relation = conceptManage.getGoRelationByObject(firstFrom);

                List<RelationItem> filterItem = relation.stream().filter(item -> item.getDestObj().equals(firstTo)).collect(Collectors.toList());

                for (RelationItem relationItem : filterItem) {

                    boolean match = modelObjLs.containsAll(relationItem.getSourceObjList());

                    if (!match) {
                        continue;
                    }

                    if (!modelObjLs.contains(relationItem.getDestObj())) {
                        continue;
                    }


                }

            }
        }
    }

}
